﻿using UnityEngine;
using System.Collections;

public class CommonMsgBox : MonoBehaviour {
    public UILabel message;
    public GameObject OKButton;
    public GameObject CancelButton;

    static CommonMsgBox _window;

    public LTEventListenerC m_MethodDeliver;

    public static CommonMsgBox window
    {
        get
        {
            if (_window == null)
            {
                UIRoot root = GameObject.Find("UI Root (2D)").GetComponent<UIRoot>();
                if (root != null)
                {
                    Camera cam = root.GetComponentInChildren<Camera>();
                    if (cam != null)
                    {
                        UIAnchor anchor = cam.gameObject.GetComponentInChildren<UIAnchor>();
                        if (anchor != null)
                        {
                            _window = LTPrefabManager.InstantiateC(PrefabTypes.CommonMsgBox, Vector3.zero, Quaternion.identity) as CommonMsgBox;
                            _window.transform.parent = anchor.transform;
                            _window.transform.localPosition = new Vector3(_window.transform.localPosition.x, _window.transform.localPosition.y, -20);
                            _window.transform.localScale = Vector3.one;
                        }
                    }
                }
            }
            return _window;
        }
    }

	// Use this for initialization
	void Start () {
	
	}

    private PopupTypes _type;
    public void ShowPopup(PopupTypes type, LTEventListenerC target)
    {
        m_MethodDeliver = target;
        _type = type;
        //MsgBoxOKBtn okButton = gameObject.GetComponentInChildren<MsgBoxOKBtn>();
        message.text = CSceneManager.manager.msgText[(int)type];
        switch (type)
        {
            case PopupTypes.FacebookConnectionSelect:
                closeType = MsgBoxCloseType.GotoLoginScene;
                OKButton.transform.localPosition = new Vector3(30f, OKButton.transform.localPosition.y, OKButton.transform.localPosition.z);
                CancelButton.transform.localPosition = new Vector3(-140f, CancelButton.transform.localPosition.y, CancelButton.transform.localPosition.z);
                OKButton.SetActive(true);
                CancelButton.SetActive(true);
                break;
            case PopupTypes.DeleteFriend:
                closeType = MsgBoxCloseType.Destroy;
                OKButton.transform.localPosition = new Vector3(30f, OKButton.transform.localPosition.y, OKButton.transform.localPosition.z);
                CancelButton.transform.localPosition = new Vector3(-140f, CancelButton.transform.localPosition.y, CancelButton.transform.localPosition.z);
                OKButton.SetActive(true);
                CancelButton.SetActive(true);
                break;
            default:
                closeType = MsgBoxCloseType.GotoLoginScene;
                OKButton.transform.localPosition = new Vector3(-50f, OKButton.transform.localPosition.y, OKButton.transform.localPosition.z);
                OKButton.SetActive(true);
                CancelButton.SetActive(false);
                break;
        }
    }

    public MsgBoxCloseType closeType;
    void OnOkClick()
    {
        switch (_type)
        {
            case PopupTypes.FacebookConnectionSelect:
                CSceneManager.manager.GoToLoadingScene();
                break;
            case PopupTypes.DeleteFriend:
                if (m_MethodDeliver != null)
                    m_MethodDeliver.OnEvent(0);
                break;
        }
    }

    void OnCancelClick()
    {
        switch (_type)
        {
            case PopupTypes.FacebookConnectionSelect:
                LoadingWindow.window.ShowShortLoading();
                RestAPI.singleton.SendFacebookInfo(ReturnCallback, true);
                break;
            case PopupTypes.DeleteFriend:
                DestroyImmediate(gameObject);
                break;
        }
    }

    void ReturnCallback(Hashtable returnData)
    {
        switch ((RestAPIProtocol)returnData["protocol"])
        {
            case RestAPIProtocol.SENDFACEBOOKINFO:
                LoadingWindow.window.HideLoading();
                CSceneManager.manager.GoToLoadingScene();
                break;
        }
    }
}
